.PHONY: build run test build-all checksum compress package

VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo 'v0.0.0')
BUILD_TIME := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
UPX := $(shell command -v upx || echo true)

build:
	go build -o bin/main ./cmd

build-all:
	@echo "Building for all platforms..."
	mkdir -p release
	GOOS=windows GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)" -o release/go-demo-windows-amd64.exe ./cmd/bastion
	GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)" -o release/go-demo-linux-amd64 ./cmd/bastion
	GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION) -X main.buildTime=$(BUILD_TIME)" -o release/go-demo-darwin-amd64 ./cmd/bastion

checksum:
	@echo "Generating SHA256 checksums..."
	cd release && \
	sha256sum go-demo-* > sha256sums.txt

compress:
	@echo "Compressing binaries with UPX..."
	cd release && \
	if [ "$$(UPX)" != "true" ]; then \
		$$(UPX) --best go-demo-*; \
	else \
		echo "UPX not found, skipping compression"; \
	fi

package:
	@echo "Creating Windows installer..."
	makensis -DVERSION=$(VERSION) installer.nsi

run:
	go run ./cmd

test:
	go test -v ./...